<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
      xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head th:include="include :: header"></head>
<style type="text/css">
    .pull-right.pagination-detail {
        display: none;
    }
</style>
<body class="hold-transition skin-blue sidebar-mini">
<section class="content-header">
    <h1>设计进度看板</h1>
</section>
<section class="content">
    <div class="row">
        <div class="col-xs-12">
            <div class="box">
                <div class="box-header">
                    <form id="queryForm" onsubmit="return false;">
                        <div class="row">
                            <div class="form-inline">
                                <div class="form-group">
                                    <label for="query-proCode">项目编号</label>
                                    <input id="query-proCode" type="text" name="proCode" class="form-control"
                                           placeholder="请输入项目编号">
                                </div>
                                <div class="form-group">
                                    <label for="query-proName">项目名称</label>
                                    <input id="query-proName" type="text" name="proName" class="form-control"
                                           placeholder="请输入项目名称">
                                </div>
                                <div class="form-group">
                                    <label for="query-demander">需方</label>
                                    <input id="query-demander" type="text" name="demander" class="form-control"
                                           placeholder="请输入需方">
                                </div>
                                <div class="form-group">
                                    <label for="query-address">地点</label>
                                    <input id="query-address" type="text" name="address" class="form-control"
                                           placeholder="请输入地点">
                                </div>
                                <div class="form-group">
                                    <label for="query-proNum">数量</label>
                                    <input id="query-proNum" type="text" name="proNum" class="form-control"
                                           placeholder="请输入数量">
                                </div>
                                <div class="form-group">
                                    <label for="query-startTime">日期</label>
                                    <input id="query-startTime" type="text" class="form-control datepicker"
                                           name="startTime"
                                           readonly="readonly"/>
                                    <label for="query-endTime"> 至 </label>
                                    <input id="query-endTime" type="text" class="form-control datepicker" name="endTime"
                                           readonly="readonly"/>
                                </div>
                                <div class="form-group">
                                    <label for="query-year">年度</label>
                                    <select id="query-year" class="form-control" name="year">
                                        <option value="" selected="selected">请选择</option>
                                    </select>
                                </div>
                                <div class="form-group">
                                    <label for="query-taskName">任务名</label>
                                    <select id="query-taskName" class="form-control" name="taskNameId">
                                        <option value="5" selected="selected">设计任务</option>
                                        <option value="6">控制意图</option>
                                        <option value="8">调试任务</option>
                                        <option value="7">成本输出</option>
                                    </select>
                                </div>
                                <div class="form-group">
                                    <label for="query-status">任务状态</label>
                                    <select id="query-status" class="form-control" name="status">
                                        <option value="1" selected="selected">未完成</option>
                                        <option value="2">已完成</option>
                                        <option value="">全部</option>
                                    </select>
                                </div>
                                <div class="form-group">
                                    <label for="query-dept">设计科室</label>
                                    <select id="query-dept" class="form-control" name="deptId">
                                        <option value="" selected="selected">请选择</option>
                                        <option th:each="dept:${depts}" th:value="${dept.id}"
                                                th:text="${dept.name}"></option>
                                    </select>
                                </div>
                                <button type="button" class="btn btn-success" id="btn-search"
                                        onclick="js.table.search(table)">
                                    <span class="Bold">搜索</span>
                                </button>
                            </div>
                        </div>
                    </form>
                </div>
                <div class="box-body">
                    <table id="bootstrap-table" style="width: 99.9%;"></table>
                </div>
            </div>
        </div>
    </div>
</section>
<div th:include="include :: footer"></div>
<script th:inline="javascript">

    let table;
    var statusName = ["", "未开始", "执行中", "已完成", "不需要"];
    var classType = ["", " label-default", " label-warning", " label-success", " label-primary"];

    function yearSelectCreate() {
        var myDate = new Date();
        var endYear = myDate.getFullYear();
        for (var i = endYear; i >= 2016; i--) {
            if (i === 2016)
                $("#query-year").append("<option value='" + i + "'>" + i + "年及以前" + "</option>");
            else
                $("#query-year").append("<option value='" + i + "'>" + i + "年" + "</option>");
        }
    }

    $(function () {
        yearSelectCreate();

        /** 设计进度列表 */
        table = js.table.init({
            url: ctx + "designTask/process/processList",
            onLoadSuccess: function (data) {
                var dataSource = $('#bootstrap-table').bootstrapTable('getData', true);
                mergeCells(dataSource, "proId", "rowNumber", 1, $('#bootstrap-table')); //合并单元格
                mergeCells(dataSource, "proId", "proCode", 1, $('#bootstrap-table')); //合并单元格
                mergeCells(dataSource, "proId", "proName", 1, $('#bootstrap-table')); //合并单元格
                mergeCells(dataSource, "proId", "demander", 1, $('#bootstrap-table')); //合并单元格
                mergeCells(dataSource, "proId", "address", 1, $('#bootstrap-table')); //合并单元格
                mergeCells(dataSource, "proId", "proNum", 1, $('#bootstrap-table')); //合并单元格
                mergeCells(dataSource, "proId", "designPlanTime", 1, $('#bootstrap-table')); //合并单元格
                mergeCells(dataSource, "proId", "comment", 1, $('#bootstrap-table')); //合并单元格
                mergeCells(dataSource, "proId", "operate", 1, $('#bootstrap-table')); //合并单元格
            },
            paginationDetailHAlign: 'right',
            columns: [
                {title: "行号", field: 'rowNumber'},
                {title: "项目编号", field: 'proCode'},
                {title: "项目名称", field: 'proName', width: '160'},
                {title: "需方", field: 'demander', width: '150'},
                {title: "安装地点", field: 'address', width: '150'},
                {title: "数量", field: 'proNum'},
                {title: "设计计划<br>完成时间", field: 'designPlanTime'},
                {
                    title: "设计科室", field: 'deptName'
                },
                {
                    title: "设计任务", field: 'firstStatus',
                    formatter: function (value, row) {
                        return '<span class="badge' + classType[value] + '">' + statusName[value] + '</span>';
                    }
                },
                {
                    title: "控制意图", field: 'secondStatus',
                    formatter: function (value) {
                        return '<span class="badge' + classType[value] + '">' + statusName[value] + '</span>';
                    }
                },
                {
                    title: "调试任务", field: 'fourthStatus',
                    formatter: function (value) {
                        return '<span class="badge' + classType[value] + '">' + statusName[value] + '</span>';
                    }
                },
                {
                    title: "成本输出", field: 'thirdStatus',
                    formatter: function (value) {
                        return '<span class="badge' + classType[value] + '">' + statusName[value] + '</span>';
                    }
                },
                {title: "备注", field: 'comment', width: '90'},
                {
                    title: "操作", field: 'operate',
                    formatter: function (value, row) {
                        let actions = [];
                        if (hasP('dsn:proBull:view')) {
                            actions.push('<a target="_blank" class="btn btn-info btn-xs" href="' + ctx + 'designTask/process/proSinglePage?proId= ' + row.proId + '"> 查看</a> ');
                        }
                        return actions.join('');
                    }
                }
            ],
            rowStyle: function (row, index) {
                if (row.status == 1) {
                    return {css: {"background-color": "rgb(255, 0, 0)"}}
                } else if (row.rn == 1) {
                    return {css: {"color": "black", "border-top-color": "#333", "border-top": "solid 1px"}}
                } else {
                    return {css: {"color": "black"}}
                }
            }
        });

        $("#bootstrap-table").on('click', ".btn-info", function () {
            $(this).css({'background-color': 'green', 'font-weight': 'bolder'});
        });

        /**
         * 生产状态改变事件
         */
        $("#query-status").on('change', function () {
            $("#queryForm input[name='statusList']").val($("#query-status").val());
        });

    });

    /**
     * 合并单元格
     * @param data  原始数据（在服务端完成排序）
     * @param fieldName 合并属性名称
     * @param fiedArry 待合并列
     * @param colspan   合并列
     * @param target    目标表格对象
     */
    function mergeCells(data, fieldName, fiedArry, colspan, target) {
        var valueArry = new Array();//数组只记录值
        for (var i = 0; i < data.length; i++) {
            for (var prop in data[i]) {
                if (prop == fieldName) {
                    var key = data[i][prop]
                    valueArry[i] = key;
                    break;
                }
            }
        }

        var numArry = new Array();//记录值出现的次数  一会就通过循环次数组  合并单元格
        var value = valueArry[0];//初始值
        var num = 0;//记录值出现的次数
        var index1 = 0;//numArry的下标
        for (var i = 0; i <= valueArry.length; i++) {
            if (i == valueArry.length) {//解决最后一次赋值
                numArry[index1] = num;
                break;
            }
            if (value == valueArry[i]) {
                num++;//list下一个值与变量value值相同时，num+1，下标不变
            } else {
                value = valueArry[i];//如果值不相等就把下一个值   赋值给value变量
                numArry[index1] = num;//把value变量之前的值 出现的次数记录下来
                num = 1;// 新的value值出现一次
                index1++;//下标+1;
            }
        }

        var index = 0;
        for (var i = 0; i < numArry.length; i++) {
            var count = numArry[i] * 1;
            $(target).bootstrapTable('mergeCells', {
                index: index,
                field: fiedArry,
                colspan: colspan,
                rowspan: count
            });
            index += count;
        }
    }
</script>
</body>
</html>
